/* eslint-disable no-multi-spaces */
const Base = require('./base.js');
const { think } = require('thinkjs');
const moment = require('moment');

module.exports = class extends Base {
  async indexAction() {
    const WeixinSerivce = this.service('weixin', 'api');
    const result = WeixinSerivce.payNotify(this.post('xml'));

    if (!result) {
      return `<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[支付失败]]></return_msg></xml>`;
    }

    const order = await this.model('pay_log').where({out_trade_no: result.out_trade_no}).find();
    
    if (think.isEmpty(order)) {
      return `<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[订单不存在]]></return_msg></xml>`;
    }
    if (order.pay_status > 0) {
      return '';
    }

    

    const detail = await this.model('figure_apply').where({user_id: order.user_id, status: ['>',2]}).find()

    if(!think.isEmpty(detail)){

      if(detail.pay_status > 0) {
        //续费
        await this.model('pay_log').where({out_trade_no: result.out_trade_no}).update({
          pay_status: 1,
          type: 2
        });

        //人物加时间
        let newDateFigure = null

        const figure = await this.model('figure').where({id: detail.figure_id}).find()

        if(moment(figure.dated_time).isBefore(moment())){
          newDateFigure = moment().add(order.time, 'M').format()
        }else{
          newDateFigure = moment(figure.dated_time).add(order.time, 'M').format()
        }
        await this.model('figure').where({id: detail.figure_id}).update({
          dated_time: newDateVip
        });
        
      }else{
        //初次认证
        await this.model('pay_log').where({out_trade_no: result.out_trade_no}).update({
          pay_status: 1,
          type: 1
        });

        //增加人物
        if(detail.figure_id){
          await this.model('figure_apply').where({user_id: order.user_id}).update({
            pay_status: order.level
          })

          await this.model('figure').where({id: detail.figure_id}).update({
            is_vip: order.level,
            dated_time: moment().add(order.time, 'M').format()
          });

        }else{
          // delete detail.id

          await this.model('figure').where({id: detail.figure_id}).update({
            dated_time: moment().add(order.time, 'M').format(),
            is_vip: order.level
          });

          await this.model('figure_apply').where({user_id: order.user_id}).update({
            pay_status: order.level
          })
        }
      }

      //vip加时间
      let newDateVip = null
      
      const user = await this.model('user').where({id: order.user_id}).find()
      if(moment(user.end_time).isBefore(moment())){
        newDateVip = moment().add(order.time, 'M').format()
      }else{
        newDateVip = moment(user.end_time).add(order.time, 'M').format()
      }
      await this.model('user').where({id: order.user_id}).update({
        end_time: newDateVip,
        is_vip: order.level,
        name: detail.name,
        company: detail.company_name,
        position: detail.position
      });
    }


    return `<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>`;
  }
};
